package com.qlmy.cn.ui.viewmodel

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.SavedStateHandle
import com.qlmy.cn.api.ApiObserver
import com.qlmy.cn.api.SERVICE
import com.qlmy.cn.api.launchUI
import com.qlmy.cn.api.toData
import com.qlmy.cn.bean.*
import com.qlmy.cn.commom.format
import com.qlmy.cn.ui.base.BaseViewModel
import com.qlmy.cn.utils.PATTERN_YYYY_MM
import java.util.*

class SummaryViewModel(private val savedStateHandle: SavedStateHandle) : BaseViewModel() {
    private val mInventoryClassificationTops: Array<InventoryClassificationTopEnum> = InventoryClassificationTopEnum.values()
    private val mRealTimeIinventoryTops: Array<RealTimeIinventoryTopEnum> = RealTimeIinventoryTopEnum.values()
    val tableUnitLiveData: MutableLiveData<String> = MutableLiveData()
    val leftTableDataLiveData: MutableLiveData<MutableList<TableDate>> = MutableLiveData()
    val topTableDataLiveData: MutableLiveData<MutableList<TableDate>> = MutableLiveData()
    val contentTableDataLiveData: MutableLiveData<MutableList<MutableList<TableDate>>> = MutableLiveData()
    fun querySortSummary(spny: Date, apiObserver: ApiObserver<MutableList<MutableList<TableDate>>>) {
        apiObserver.success = { contentTableDataLiveData.value = it }
        launchUI({
            val leftTableData = mutableListOf<TableDate>()
            val contentTableData = mutableListOf<MutableList<TableDate>>()
            var index = 0
            var unit: String? = null
            SERVICE.querySortSummary(spny.format(PATTERN_YYYY_MM)).toData()?.map { StockMonthBean(it) }?.forEach { item ->
                if (unit == null)
                    unit = item.zldw
                // 如果是小计这说明该行是统计行，统计行使用pzxz作为显示
                val contentTableItemData = mutableListOf<TableDate>()
                contentTableData.add(contentTableItemData)
                if (item.pzmc.contains("小计")) {
                    index = 0
                    leftTableData.add(TableDate(item.pzxz, TableType.GROUP))
                    mInventoryClassificationTops.forEach { contentTableItemData.add(TableDate(item[it.key], TableType.GROUP)) }
                } else {
                    val tableType = if (index % 2 == 0) TableType.ROW_WHITE else TableType.ROW_GREY
                    leftTableData.add(TableDate(item.pzmc, tableType))
                    mInventoryClassificationTops.forEach { contentTableItemData.add(TableDate(item[it.key], tableType)) }
                    index += 1
                }
            }
            leftTableDataLiveData.value = leftTableData
            tableUnitLiveData.value = unit ?: ""
            if (topTableDataLiveData.value == null)
                topTableDataLiveData.value = mInventoryClassificationTops.mapTo(mutableListOf()) { TableDate(it.title) }
            contentTableData
        }, apiObserver)
    }

    fun queryReserveSummary(sbny: Date, apiObserver: ApiObserver<MutableList<MutableList<TableDate>>>) {
        apiObserver.success = { contentTableDataLiveData.value = it }
        launchUI({
            val leftTableData = mutableListOf<TableDate>()
            val contentTableData = mutableListOf<MutableList<TableDate>>()
            var index = 0
            var unit: String? = null
            SERVICE.queryReserveSummary(sbny.format(PATTERN_YYYY_MM)).toData()?.map { StockMonthBean(it) }?.forEach { item ->
                if (unit == null)
                    unit = item.zldw
                val contentTableItemData = mutableListOf<TableDate>()
                contentTableData.add(contentTableItemData)
                val tableType = if (index % 2 == 0) TableType.ROW_WHITE else TableType.ROW_GREY
                leftTableData.add(TableDate(item.dwmc, tableType))
                mRealTimeIinventoryTops.forEach { contentTableItemData.add(TableDate(item[it.key], tableType)) }
                index += 1
            }
            leftTableDataLiveData.value = leftTableData
            tableUnitLiveData.value = unit
            reserveSummaryTop()
            contentTableData
        }, apiObserver)
    }

    private fun reserveSummaryTop() {
        if (topTableDataLiveData.value == null) {
            topTableDataLiveData.value = mutableListOf<TableDate>().apply {
                RealTimeIinventoryTopEnum.values().forEach { realTimeIinventoryTopEnum ->
                    if (realTimeIinventoryTopEnum.group != null) {
                        val topTableGroupData = find { it.name == realTimeIinventoryTopEnum.group.title }
                                ?: TableDate(realTimeIinventoryTopEnum.group.title, subTableDates = mutableListOf()).apply { add(this) }
                        topTableGroupData.subTableDates?.add(TableDate(realTimeIinventoryTopEnum.title))
                    } else {
                        add(TableDate(realTimeIinventoryTopEnum.title))
                    }
                }
            }
        }
    }
}